package cn.hiapi.shopkeeper.service.activity;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityVote;
import cn.hiapi.shopkeeper.entity.activity.ActivityVoteItem;
import cn.hiapi.shopkeeper.entity.activity.ActivityVoteOther;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.jpa.activity.ActivityVoteJpa;
import cn.hiapi.shopkeeper.request.CreateOrderRequest;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author AdinZ
 * @date 2023/2/3 1:24
 */
@Service
public class ActivityVoteService extends AbsActivityService<ActivityVoteJpa, ActivityVote, Long> {
    protected ActivityVoteService(DispatchContext dispatchContext) {
        super(dispatchContext);
    }

    @Override
    public ActivityType getActivityType() {
        return ActivityType.VOTE;
    }

    @Override
    public JSONObject getExpand(long aid) {
        JSONObject expand = super.getExpand(aid);
        if (expand != null) {
            expand.put("items", this.dispatchContext.getServiceOne(ActivityVoteItemService.class).findByAid(aid));
            expand.put("others", this.dispatchContext.getServiceOne(ActivityVoteOtherService.class).findByAid(aid));
        }
        return expand;
    }

    @Override
    @Transactional
    public BasicEntity saveExpand(long aid, JSONObject data) {
        ActivityVote vote = data.toJavaObject(ActivityVote.class);
        if (vote.getVoteNum() < 1) throw new BasicException("投票数量错误");
        vote.setAid(aid);
        List<ActivityVoteOther> others = data.getJSONArray("others").toJavaList(ActivityVoteOther.class);
        if (others == null || others.isEmpty()) throw new BasicException("请添加投票选项");
        for (ActivityVoteOther other : others) {
            if (StringUtils.isBlank(other.getTitle())) {
                throw new BasicException("选项标题错误");
            }
            if (StringUtils.isBlank(other.getThumb())) {
                throw new BasicException("选项图片错误");
            }
            other.setAid(aid);
            if (others.stream().filter(e -> e.getOtherId() == other.getOtherId()).count() > 1)
                throw new BasicException("选项id错误");
        }
        if (vote.getAward() == 1) {
            List<ActivityVoteItem> items = data.getJSONArray("items").toJavaList(ActivityVoteItem.class);
            if (items == null || items.isEmpty()) throw new BasicException("请添加投票奖励");

            for (ActivityVoteItem item : items) {
                if (StringUtils.isBlank(item.getTitle())) {
                    throw new BasicException("请输入奖品标题");
                }
                if (StringUtils.isBlank(item.getThumb())) {
                    throw new BasicException("请输入奖品图片");
                }
                if (StringUtils.isBlank(item.getBody())) {
                    throw new BasicException("请输入感谢语");
                }
                if (others.stream().filter(e -> e.getOtherId() == item.getOtherId()).count() == 0)
                    throw new BasicException(item.getTitle() + "奖品与选项关联错误");
                item.setAid(aid);
            }
            items.forEach(e -> e.setId(null));
            this.dispatchContext.getServiceOne(ActivityVoteItemService.class).save(items);
        }
        this.save(vote);
        this.dispatchContext.getServiceOne(ActivityVoteOtherService.class).save(others);
        return vote;
    }

    @Override
    @Transactional
    public int deleteByAid(long aid) {
        this.dispatchContext.getServiceOne(ActivityVoteItemService.class).deleteByAid(aid);
        this.dispatchContext.getServiceOne(ActivityVoteOtherService.class).deleteByAid(aid);
        return ResUtils.getDefault(this.repository.deleteByAid(aid));
    }

    @Override
    public ActivityOrder createOrder(BasicEntity basic, Activity activity, Map<String, Long> incrementStock, CreateOrderRequest request, Token<Object> token) {
        return null;
    }
}
